package de.pdbm.swt.aufgabe3;

import java.util.ArrayList;
import java.util.List;

public class EntityManager {

	private static final EntityManager INSTANCE = new EntityManager();

	List<Entity> entitylist = new ArrayList<Entity>();

	private EntityManager() {

	}

	public static EntityManager getInstance() {

		return INSTANCE;

	}

	// TODO Richtig machen!
	public Entity find(java.lang.Class<? extends Entity> clazz,
			java.lang.String id) throws EntityNotFoundException {
		
		for (Entity rofl : entitylist) {

			if (rofl.getId() == id
					&& rofl.getClass().getInterfaces()[0].equals(clazz)) {

				return rofl;
			}
		}
		throw new EntityNotFoundException();

	}

	public void remove(Entity o) {
		entitylist.remove(o);

	}

	public void save(Entity entity) throws java.lang.IllegalArgumentException {
		entitylist.size();
		if (entitylist.isEmpty()) {
			entitylist.add(entity);
		} else {

			for (int i = 0; i < entitylist.size(); i++) {
				if (entitylist.get(i).getId() == entity.getId()
						&& entitylist.get(i).getClass() == entity.getClass()) {
					break;
				}

			}
			entitylist.add(entity);
		}
	}
}